package cn.fulong.smebj.admin.controller;

import cn.fulong.frame.config.Result;
import cn.fulong.smebj.admin.service.QuestionService;
import cn.fulong.smebj.admin.util.GetUUIDUtil;
import cn.fulong.smebj.entity.Answer;
import cn.fulong.smebj.entity.AnswerImg;
import cn.fulong.smebj.entity.Question;
import cn.fulong.smebj.mapper.AnswerImgMapper;
import cn.fulong.smebj.mapper.AnswerMapper;
import cn.fulong.smebj.mapper.QuestionMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("试题")
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Value("${application.upload-path}")
    private String path;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private AnswerImgMapper answerImgMapper;

    @ApiOperation("教师用户-上传试题图片")
    @PostMapping(value = "/uploadTitlePicture")
    public Map<String, Object> uploadTitlePicture(MultipartFile file) throws Exception{
        Map<String, Object> resultMap = new HashMap<>();
        if (!file.isEmpty()) {
            String originalFilename = file.getOriginalFilename();// 获取文件名称
            String fileName = originalFilename.substring(0, originalFilename.indexOf("."));// 获取前缀
            String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));// 获取后缀
            if(!suffixName.equals(".jpg")&&!suffixName.equals(".jpeg")&&!suffixName.equals(".png")&&!suffixName.equals(".bmp")){
                resultMap.put("code", 500);
                resultMap.put("msg", "图片格式不正确，请上传jpg、jpeg、png或bmp格式的图片文件");
                return resultMap;
            }
            if(file.getSize()>1024*1024*5){
                resultMap.put("code", 500);
                resultMap.put("msg", "图片大小不能超过5MB");
                return resultMap;
            }
            String newFileName = GetUUIDUtil.getUUID() + suffixName;//
//            // 上传图片
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path +  "/question/" + newFileName));
            resultMap.put("code", 200);
            resultMap.put("msg", "上传成功");
            resultMap.put("originName", fileName);
            resultMap.put("data", newFileName);
            resultMap.put("name", newFileName);
        }
        return resultMap;
    }

    @ApiOperation("教师用户-试题列表")
    @PostMapping(value = "/questionList")
    public Result<Object> questionList(Integer pageNo, Integer pageSize) {
        return questionService.questionList(pageNo,pageSize);
    }

    @ApiOperation("教师用户-已答题试题列表")
    @PostMapping(value = "/questionAnswerList")
    public Result<Object> questionAnswerList(Integer pageNo, Integer pageSize) {
        return questionService.questionAnswerList(pageNo,pageSize);
    }

    @ApiOperation("教师用户-新增试题")
    @PostMapping(value = "/addQuestion")
    public Result<Object> addQuestion(@RequestBody Question question) {
        return questionService.addQuestion(question);
    }

    @ApiOperation("教师用户-修改试题")
    @PostMapping(value = "/editQuestion")
    public Result<Object> editQuestion(@RequestBody Question question) {
        return questionService.editQuestion(question); 
    }


    @ApiOperation("教师用户-查看试题")
    @PostMapping(value = "/lookQuestion")
    public Result<Object> lookQuestion(Long id) {
        Question question = questionMapper.selectById(id);
        return questionService.addQuestion(question);
    }

    @ApiOperation("教师用户-删除试题")
    @PostMapping(value = "/deleteQuestion")
    public Result<Object> deleteQuestion(Long id) {
        return questionService.deleteQuestion(id);
    }

    @ApiOperation("查看试题结果")
    @PostMapping(value = "/lookResult")
    public Result<Object> lookResult(Long questionId,Long userId) {
        Question question = questionMapper.selectById(questionId);
        List<Answer> answers = answerMapper.query(questionId,userId);
        List<AnswerImg> answerImg = answerImgMapper.selectByQuestion(answers.get(0).getId());

        Map<String,Object> result = new HashMap<>();
        result.put("question",question.getFilePath());
        result.put("answer",answerImg);
        result.put("pizhu",answers.get(0));
        return Result.success(result);
    }

}
